package projectpackage;

import javax.swing.JFrame;
import javax.swing.JSplitPane;
import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTextField;

import org.omg.CORBA.portable.InputStream;

import java.awt.Component;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

public class ClientInterface implements Observer {
	private static final String TEXT_SUBMIT = "text-submit";
	private static final String INSERT_BREAK = "insert-break";

	public JFrame frame;
	private JTextField ServerTextField;
	JButton btnConnect;
	JTextArea textArea;
	Thread thread;
	Client client;
	public InputStream inputStream;
	JTextArea ChatWindow;
	JScrollPane scrollPane;
	JTextArea MessageWindow;
	boolean connectionactivate = true;
	JTextArea OnlineWindow;

	/**
	 * Launch the application.
	 */
	/**
	 * Initialize the contents of the frame.
	 */
	public ClientInterface() {	
		
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		JMenuItem mntmExit = new JMenuItem("Exit");
		mnFile.add(mntmExit);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		frame.getContentPane().add(splitPane, BorderLayout.CENTER);
		
		JSplitPane splitPane_1 = new JSplitPane();
		splitPane_1.setAlignmentX(Component.CENTER_ALIGNMENT);
		splitPane_1.setPreferredSize(new Dimension(300, 30));
		splitPane.setLeftComponent(splitPane_1);
		
		ServerTextField = new JTextField();
		ServerTextField.setName("ServerTextField");
		ServerTextField.setMinimumSize(new Dimension(300, 20));
		splitPane_1.setLeftComponent(ServerTextField);
		ServerTextField.setPreferredSize(new Dimension(300, 20));
		ServerTextField.setColumns(10);
		
		btnConnect = new JButton("Connect");
		btnConnect.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e)
			{
				if(connectionactivate)
				{					
					try {
						String serverAdress = ServerTextField.getText();
						client = new Client(serverAdress);					
						client.Connect();
						client.addObserver(client.getframe());
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
		});
		splitPane_1.setRightComponent(btnConnect);
		
		JSplitPane splitPane_2 = new JSplitPane();
		splitPane.setRightComponent(splitPane_2);
		
		JSplitPane splitPane_3 = new JSplitPane();
		splitPane_3.setOrientation(JSplitPane.VERTICAL_SPLIT);
		splitPane_2.setLeftComponent(splitPane_3);
		
		scrollPane = new JScrollPane();
		scrollPane.setFocusTraversalPolicyProvider(true);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setPreferredSize(new Dimension(300, 150));
		scrollPane.setMinimumSize(new Dimension(300, 150));
		scrollPane.setMaximumSize(new Dimension(300, 150));
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		splitPane_3.setLeftComponent(scrollPane);
		
		ChatWindow = new JTextArea();
		ChatWindow.setEditable(false);
		ChatWindow.setPreferredSize(new Dimension(280, 150));
		
		scrollPane.setViewportView(ChatWindow);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane_1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		splitPane_3.setRightComponent(scrollPane_1);
		
		MessageWindow = new JTextArea();
		
		InputMap input = MessageWindow.getInputMap();
		KeyStroke enter = KeyStroke.getKeyStroke("ENTER");
		KeyStroke shiftEnter = KeyStroke.getKeyStroke("shift ENTER");
		input.put(shiftEnter, INSERT_BREAK);  // input.get(enter)) = "insert-break"
		input.put(enter, TEXT_SUBMIT);

		ActionMap actions = MessageWindow.getActionMap();
		actions.put(TEXT_SUBMIT, new AbstractAction() {
			private static final long serialVersionUID = -6774550748559248702L;
		@Override
		    public void actionPerformed(ActionEvent e) {
		           submitText();
		    }
		});
		
		scrollPane_1.setViewportView(MessageWindow);
		
		JSplitPane splitPane_4 = new JSplitPane();
		splitPane_4.setMinimumSize(new Dimension(100, 200));
		splitPane_4.setPreferredSize(new Dimension(100, 200));
		splitPane_4.setOrientation(JSplitPane.VERTICAL_SPLIT);
		splitPane_2.setRightComponent(splitPane_4);
		
		JButton btnSend = new JButton("Send");
		btnSend.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if(!connectionactivate)
				{
					String text = MessageWindow.getText();
					client.Write(text.trim());
					MessageWindow.setText("");
				}
				else
				{
					ChatWindow.append("\n" + MessageWindow.getText());
					ChatWindow.select(ChatWindow.getHeight()+1000, 0);					
					ChatWindow.setPreferredSize(new Dimension(ChatWindow.getWidth(), ChatWindow.getLineCount() * 16));
					MessageWindow.setText("");
				}
			}			
		});		
		
		btnSend.setMaximumSize(new Dimension(57, 30));
		splitPane_4.setRightComponent(btnSend);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane_2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane_2.setPreferredSize(new Dimension(100, 150));
		splitPane_4.setLeftComponent(scrollPane_2);
		
		OnlineWindow = new JTextArea();
		OnlineWindow.setPreferredSize(new Dimension(100, 150));
		scrollPane_2.setViewportView(OnlineWindow);
	}
	public void submitText()
	{
		if(!connectionactivate)
		{		
			String text = MessageWindow.getText();
			client.Write(text.trim());
			MessageWindow.setText("");		
		}
		else
		{		
			ChatWindow.append("\n" + MessageWindow.getText());
			ChatWindow.select(ChatWindow.getHeight()+1000, 0);					
			ChatWindow.setPreferredSize(new Dimension(ChatWindow.getWidth(), ChatWindow.getLineCount() * 16));
			MessageWindow.setText("");				
		}
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		ClientState c = (ClientState)arg1;
		if(c.isconnected){
			connectionactivate = !c.isconnected;
			btnConnect.setEnabled(!c.isconnected);			
		}
		else {
			connectionactivate = !c.isconnected;
			btnConnect.setEnabled(!c.isconnected);
		}
		
		//textArea.setText(textArea.getText() + "\n" + c.chatWindow);
		
		if(!c.onlychangingvector)
		{
			ChatWindow.append(c.chatWindow + "\n");
			ChatWindow.select(ChatWindow.getHeight()+1000, 0);		
			ChatWindow.setPreferredSize(new Dimension(ChatWindow.getWidth(), ChatWindow.getLineCount() * 16));
		}
		
		OnlineWindow.setText(c.onlineWindow);
	}
}
